package com.example.service.webhook.business.jingdong.repository;

import com.example.service.webhook.business.alipay.entity.AlipayWealthEntity;
import com.example.service.webhook.business.jingdong.entity.JdBindCardEntity;
import com.example.service.webhook.business.jingdong.entity.JdWealthEntity;
import com.example.service.webhook.repository.base.BaseDAO;
import com.example.service.webhook.util.DateUtil;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;

/**
 * @author liyang
 * @email liyang@51dojo.com
 * @create 2017-10-24 上午10:03
 * @description 京东绑定卡信息
 **/
@Repository
public class JdBindCardRepository extends BaseDAO<JdBindCardEntity>{

    @Resource(name="templateJingdong")
    private JdbcTemplate jdbcTemplate;

    public JdbcTemplate getJdbcTemplate() {
        return jdbcTemplate;
    }

    public void saveBindCard(JdBindCardEntity entity)throws Exception  {
        entity.setCreateTime(DateUtil.getCurrentDate());
        entity.setLastModifyTime(DateUtil.getCurrentDate());
        this.add(entity);
    }

    public JdBindCardEntity getBindCard(String userId, String mappingId) throws Exception {
        String sql = "";
        String tableName = "t_jd_bindcard";
        sql = "select * from " + tableName + " where userId=? and mappingId=?";
        JdBindCardEntity pojo = null;
        try{
            pojo = (JdBindCardEntity) queryOne(sql, new Object[] {userId,mappingId}, new JdBindCardEntity());
        }catch(EmptyResultDataAccessException e){
            return null;
        }

        return pojo;
    }

    public void deleteBindCard(String userId,String mappingId){
        String tableName = "t_jd_bindcard";
        StringBuilder sql = new StringBuilder("DELETE FROM ").append(tableName).append(" WHERE ").
                append("userId='").append(userId).append("'").
                append("and mappingId='").append(mappingId).append("'");

        this.getJdbcTemplate().update(sql.toString());
    }

    public void updateBindCard(JdBindCardEntity entity) throws Exception  {
        entity.setLastModifyTime(DateUtil.getCurrentDate());
        this.modify(entity);
    }
}
